var localhost = '172.16.5.236'
function AjaxCityPlan(){
    /*1、post方式发送数据*/
    $.ajax({
        url:"http://"+localhost+":8000/web/kmeans/",                 // 相当于form 中的 action
        type:'POST',                  // 相当于form 中的 method
        data:{'key':'value'},         // 数据
        dataType: "json",             // 注意这个定义的是返回值的类型，不是发送数据的类型，如果返回类型不是json类型，则会进入error函数
        headers: { "X-CSRFToken": "{{ csrf_token }}" },//Django中表单提交都需要csrf验证
          //创建图标样式
       
        success: function (data) {   
            var track_feature = (new ol.format.GeoJSON({featureProjection: 'EPSG:3857'})).readFeatures(data);
            // 后端返回成功后的回调函数,data为后端传来的数据
            var source=new ol.source.Vector({
                features: track_feature,
            });
            vectorLayer_city = new ol.layer.Vector({
                source: source,
                style: function (feature,resolution){
                    // console.log(feature.get("pickAndDrop"));
                    var bu = feature.get("pickAndDrop");
                    var style = null;
                    if (bu == "Pick") {
                        style = new ol.style.Style({
                            image: new ol.style.Circle({
                                radius: 5,
                                fill: new ol.style.Fill({
                                    color: 'red'
                                })
                            })
                        });
                    }
                    else {
                        style = new ol.style.Style({
                            image: new ol.style.Circle({
                                radius: 5,
                                fill: new ol.style.Fill({
                                    color: 'blue'
                                })
                            })
                        });
                    }
                    return [style]
                }
            });
            

            for(var i in data.features){
                var POIinBuffer = 0;
                POIin=new Array();
                var coordinates = data.features[i].geometry.coordinates;
                var point = turf.point([coordinates[0], coordinates[1]]);
                buffered = turf.buffer(point,1000,{units: 'meters'});
                b = (new ol.format.GeoJSON({featureProjection: 'EPSG:3857'})).readFeature(buffered);
               
                for(var p in poi_features){
                    var coor = poi_features[p].values_.geometry.flatCoordinates;
                    var test=ol.proj.transform([coor[0], coor[1]], 'EPSG:3857', 'EPSG:4326')
                    //poi从3857坐标系转4326
                    var point1 = turf.point([test[0],test[1]]);
                    var psnPOI = turf.booleanPointInPolygon(point1,buffered);  
                    if (psnPOI === true) {
                        POIinBuffer++;
                        POIin.push(psnPOI);
                    }
                    else{
                        POIin.push(psnPOI);
                    }
                }
                POInumber = new Array();
                POInumber.push(POIinBuffer);
                b.setProperties({"POInumber":POInumber,"POIin":POIin});
                source_buffer.addFeature(b);
            }
            buffer= new ol.layer.Vector({
                source: source_buffer
            });
            buffer.name='buffer_layer';
            map.addLayer(buffer);
            map.addLayer(vectorLayer_city);
        },
        error:function(){
            alert('没有获取数据')
        }
    })
};

function removeAllOverlay(map){
    map.getOverlays().getArray().slice(0).forEach(function(overlay) {
        map.removeOverlay(overlay);
    });
}//清除所有点

//POI表格添加行
function createRow(poiclass,poiname){
	var tbody=document.getElementById("tbody");
	var tr = document.createElement('tr');
    var td1 = document.createElement('td');
    var td2 = document.createElement('td');
    //获取元素内容
    td1.innerHTML = poiclass;
    td2.innerHTML = poiname;
    //添加内容到表格中
    tr.append(td1);
    tr.append(td2);
    tbody.append(tr);
}

//表格清空除了表头所有行
function formReset()
{
    $("#POItable tr:not(:first)").empty("");//删除 除第一个tr后的所有tr
}